<template>
  <div class="login-container">
    <div class="login-box">
      <div style="font-weight: bold; font-size: 24px; text-align: center; margin-bottom: 30px; color: black">
        创新创业学分认证平台(学生登录)
      </div>
      <el-form :model="data.form"  ref="formRef" :rules="data.rules">
        <el-form-item prop="username">
          <el-input :prefix-icon="User" size="large" v-model="data.form.username" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input :prefix-icon="Lock" size="large" v-model="data.form.password" placeholder="请输入密码" show-password />
        </el-form-item>
        <el-form-item prop="role">
          <el-select size="large" style="width: 100%" v-model="data.form.role" disabled>
            <template #prefix>
              <el-icon><Avatar /></el-icon>
            </template>
            <el-option value="STUDENT" label="学生"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button size="large" type="primary" style="width: 100%" @click="login">登 录</el-button>
        </el-form-item>
      </el-form>
      <div style="text-align: right;">
        前往管理端？请 <a href="/adminLogin">点击跳转</a>
      </div>
    </div>

  </div>
</template>

<script setup>
  import { reactive, ref } from "vue";
  import {User, Lock, Stamp} from "@element-plus/icons-vue";
  import {ElMessage} from "element-plus";
  import router from "@/router";
  import alovaInstance from "@/utils/request";

  const data = reactive({
    form: {
      role: 'STUDENT'
    },
    rules: {
      username: [
        { required: true, message: '请输入账号', trigger: 'blur' },
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
      ],
    }
  })

  const formRef = ref()

  // 点击登录按钮的时候会触发这个方法
  const login = () => {
    formRef.value.validate((valid => {
      if (valid) {
        alovaInstance.Post('manager/student/login', data.form).then(res => {
              if (res.code === '200') {
                ElMessage.success("登录成功");
                setTimeout(() => {
                  /*location.href = '/manager/home'*/
                  router.push('/manager/home')
                },1000)
                localStorage.setItem('credit-system-user', JSON.stringify(res.data))
              }
            }).catch(error => {
              console.error('请求失败:', error);
              ElMessage.error(error.message);
            });
      }
    })).catch(error => {
      console.error(error)
    })
  }

</script>

<style scoped>
.login-container {
  height: 100vh;
  overflow:hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(to top, #c5e466, #63c6ea);
  background-size: cover;

}
.login-box {
  width: 420px;
  padding: 60px 35px;
  border-radius: 5px;
  box-shadow: 0 0 10px rgba(0, 0, 0.1);
  background-color: rgba(255, 255, 255, .5);
}
</style>